/* @(#)FseDocumentSection.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fse.model;

import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.flywheelms.library.fmm.serialization.FseDocumentSerialization;
import com.flywheelms.library.fse.FseDocumentSectionType;
import com.flywheelms.library.fse.enumerator.FseContentModificationState;
import com.flywheelms.library.fse.enumerator.FseLockModificationState;
import com.flywheelms.library.fse.enumerator.FseStyleModificationState;

// will be abstract
public abstract class FseDocumentSectionParagraphEditorContent extends FseDocumentSection {
	
	private int initialParagraphFocusIndex;
	private int initialParagraphFocusCursorPosition;
	private LinkedList<FseParagraph> paragraphList = new LinkedList<FseParagraph>();
	private FseLockModificationState lockModificationState = FseLockModificationState.UNCHANGED;
	private FseContentModificationState contentModificationState = FseContentModificationState.UNCHANGED;
	private FseStyleModificationState styleModificationState = FseStyleModificationState.UNCHANGED;
	
	public FseDocumentSectionParagraphEditorContent(FseDocumentSectionType aDocumentSectionType, JSONObject aJsonObject) {
		super(aDocumentSectionType);
		try {
			this.initialParagraphFocusIndex = aJsonObject.getInt(FseDocumentSerialization.key__PARAGRAPH_SECTION__INITIAL_PARAGRAPH_FOCUS);
			this.initialParagraphFocusCursorPosition = aJsonObject.getInt(FseDocumentSerialization.key__PARAGRAPH_SECTION__INITIAL_CURSOR_POSITION);
			this.lockModificationState = FseLockModificationState.getObjectForName(aJsonObject.getString(FseDocumentSerialization.key__DOCUMENT_SECTION__LOCK_MODIFICATION_STATE));
			this.contentModificationState = FseContentModificationState.getObjectForName(aJsonObject.getString(FseDocumentSerialization.key__DOCUMENT_SECTION__CONTENT_MODIFICATION_STATE));
			this.styleModificationState = FseStyleModificationState.getObjectForName(aJsonObject.getString(FseDocumentSerialization.key__DOCUMENT_SECTION__STYLE_MODIFICATION_STATE));
			JSONArray theJsonArray = aJsonObject.getJSONArray(FseDocumentSerialization.key__PARAGRAPH_SECTION__PARAGRAPH_ARRAY);         
			for(int i=0;i < theJsonArray.length();i++){                                     
			    JSONObject theParagraphJsonObject = theJsonArray.getJSONObject(i);
				this.paragraphList.add(new FseParagraph(theParagraphJsonObject));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public FseDocumentSectionParagraphEditorContent(FseDocumentSectionType aDocumentSectionType) {
		super(aDocumentSectionType);
	}

	public LinkedList<FseParagraph> getParagraphList() {
		return this.paragraphList;
	}
	public void setParagraphList(LinkedList<FseParagraph> paragraphList) {
		this.paragraphList = paragraphList;
	}
	public int getInitialParagraphFocusIndex() {
		return this.initialParagraphFocusIndex;
	}
	public void setInitialParagraphFocusIndex(int initialParagraphFocusIndex) {
		this.initialParagraphFocusIndex = initialParagraphFocusIndex;
	}
	public int getInitialParagraphFocusCursorPosition() {
		return this.initialParagraphFocusCursorPosition;
	}
	public void setInitialParagraphFocusCursorPosition(
			int initialParagraphFocusCursorPosition) {
		this.initialParagraphFocusCursorPosition = initialParagraphFocusCursorPosition;
	}
	
	@Override
	public JSONObject getJsonObject() {
		JSONObject theJsonObject = new JSONObject();
		try {
			theJsonObject.put(FseDocumentSerialization.key__PARAGRAPH_SECTION__INITIAL_PARAGRAPH_FOCUS, this.initialParagraphFocusIndex);  // TODO - should only be updated if there are other substantive changes  SDS
			theJsonObject.put(FseDocumentSerialization.key__PARAGRAPH_SECTION__INITIAL_CURSOR_POSITION, this.initialParagraphFocusCursorPosition);  // TODO - should only be updated if there are other substantive changes  SDS
			theJsonObject.put(FseDocumentSerialization.key__DOCUMENT_SECTION__LOCK_MODIFICATION_STATE, this.lockModificationState.toString());
			theJsonObject.put(FseDocumentSerialization.key__DOCUMENT_SECTION__CONTENT_MODIFICATION_STATE, this.contentModificationState.toString());
			theJsonObject.put(FseDocumentSerialization.key__DOCUMENT_SECTION__STYLE_MODIFICATION_STATE, this.styleModificationState.toString());
			JSONArray theJsonArray = new JSONArray();
			for (FseParagraph theParagraph : this.paragraphList) {
				theJsonArray.put(theParagraph.getJsonObject());
			}
			theJsonObject.put(FseDocumentSerialization.key__PARAGRAPH_SECTION__PARAGRAPH_ARRAY, theJsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return theJsonObject;
	}

	public FseLockModificationState getLockModificationState() {
		return this.lockModificationState;
	}
	
	public void setLockModificationState(FseLockModificationState aModificationState) {
		this.lockModificationState = aModificationState;
	}

	public FseContentModificationState getContentModificationState() {
		return this.contentModificationState;
	}
	
	public void setContentModificationState(FseContentModificationState aModificationState) {
		this.contentModificationState = aModificationState;
	}

	public FseStyleModificationState getStyleModificationState() {
		return this.styleModificationState;
	}
	
	public void setStyleModificationState(FseStyleModificationState aModificationState) {
		this.styleModificationState = aModificationState;
	}

}
